<?php
defined('_XR_EXEC') or die();
class framework_mvcSite{
	/**
	 * MVC对应的模板、缓存、编译文件路径(数组方式存放)
	 *
	 * @var array
	 */

	/**
	 * 模块参数
	 *
	 * @var array
	 */
	
	public static function &singleton(){
		static $instance;
		if(!isset($instance)){
			$instance=new framework_mvcSite();
		}
		return $instance;
	}
 
	/**	
	 * 构造函数
	 * 
	 */
	function __construct() {
		# 启用session
		$sess = session::singleton();
		# 设置时区
		ini_set('date.timezone', factory::getcfg('timezone', 'Asia/Shanghai'));
		header('Content-type: text/html; charset="utf-8"');  
		
		# 开户gzip
		$this->gzip();		
	}
	
	/**
	 * 执行模块
	 */
	public function execute(){
	 
		#1-初始化获取访问者客户端设备类型
    $this->parseTerminel();
		
		#2-根据设备类型，获取一些默认参数
		$this->parseSetDefaultPageArgs();		
		
		#3-获取请求变量、得到控制器视图目录
		$this->parseRequest();
		
		#4-加载并实现controller类方法
		$this->execController();
		
		#5-模板输出处理
		$this->displayView();
		
		#框架事件管理没有做好之前，先在这里结束
		exit();
	}
	
	#1-初始化获取访问者客户端设备类型
	public function parseTerminel(){
		$sess = session::singleton();
		### 特殊处理，强制模板显示
		if(isset($_GET['forceuseragent'])){
			if($_GET['forceuseragent'] == 'ipad'){
				return $sess->set('system_useragent','ipad');
			}else if($_GET['forceuseragent'] == 'iphone'){
				return $sess->set('system_useragent','iphone');
			}else if($_GET['forceuseragent'] == 'web'){
				return $sess->set('system_useragent','web');
			}
		}
		if(isset($_SESSION['system_useragent'])){
			return $sess->get('system_useragent');
		}
		if(isset($_SERVER['HTTP_USER_AGENT'])){
			$ua=strtolower($_SERVER['HTTP_USER_AGENT']);
		}else{
			$ua='web';
		}
		# 今后要考虑一下安卓平台的
		if(strpos($ua,'ipad')){
			return $sess->set('system_useragent','ipad');
		}else if(strpos($ua,'iphone')){
			return $sess->set('system_useragent','iphone');
		}else if(strpos($ua,'android')){
			return $sess->set('system_useragent','android');
		}else{
			return $sess->set('system_useragent','web');
		}
	}
	#2-根据设备类型，获取一些默认参数
	public function parseSetDefaultPageArgs(){
		$autoloadflag=factory::getcfg('autoload',true,'view');
		$de_configs=factory::getcfg('views',array(),'view');
		if(!$autoloadflag){
			$who = factory::getcfg('default_views','web','view');
		}else{
			$sess = session::singleton();
			$who = $sess->get('system_useragent');
			if(!isset($de_configs[$who])){
				$who = factory::getcfg('default_views','web','view');
			}
		}
		if(!is_array($de_configs) || !isset($de_configs[$who])){
			Errors::Go('view_config参数配置有误');
		}
		return R::singleton()->view_default = $de_configs[$who];
	}
	#3-获取请求变量、得到控制器视图目录
	public function parseRequest(){
		$default_config = R::singleton()->view_default;
		# 构建好get参数
		$_GET['controller'] = !empty ($_GET['controller']) ? strtolower($_GET['controller']) : $default_config['controller'];
		$_GET['action'] = !empty ($_GET['action']) ? strtolower($_GET['action']) : $default_config['action']; 
		$_GET['reqtype'] = !empty ($_GET['reqtype']) ? strtolower($_GET['reqtype']) : $default_config['reqType'];
		$_GET['system_useragent']=$default_config['template'];
		
		
		if(isset($_GET['urlarguments'])){
			# 拆分可能出现多个值的action参数
			$_GET['urlargument'] = explode('/',$_GET['urlarguments']);
		}
		# 做一些format::addslash_deep操作。。。。。。。。。。。。 
		R::singleton()->gets = format::addslash_deep($_GET); 
		R::singleton()->posts = format::addslash_deep($_POST);
		$a_gets = R::singleton()->gets;
		# 默认使用什么模板
		
		R::singleton()->view_direc = array(
			'controller_file'=>XR_APP_CONTROLLER.$a_gets['controller'].'_controller.php',
			'controller_action'=>$a_gets['action'].'__',
			'templateDir'=>XR_APP_VIEW.$a_gets['system_useragent'],
			'compileDir'=>XR_APP_VIEW_C.$a_gets['system_useragent']
		);
	}
	#4-加载并实现controller类方法
	public function execController(){
		$_direc = R::singleton()->view_direc;
		$_gets  = R::singleton()->gets;
		# 加载并实现controller类方式
		if (file_exists($_direc['controller_file'])) {
			include_once ($_direc['controller_file']);
			$controller_class = $_gets['controller'].'_controller';
			if (class_exists($controller_class)) {
        $instance =  new  $controller_class ();
				if( method_exists($instance,$_direc['controller_action']) ){
					$action_method = $_direc['controller_action'];
					# 执行控制器方法
					return $instance -> $action_method();
				}
			}else{
				Errors::Go('没有找到控制器处理类 class '.$controller_class);
			}
		}
	}
	#5-模板输出处理
	public function displayView(){
		//只有当开启了页面输出，并且请求模式为html则会输出页面
		if(R::singleton()->view_display === true && R::singleton()->gets['reqtype']=="html"){
			$____direc = R::singleton()->view_direc;
			$____gets	= R::singleton()->gets;
			$____view_tpl = $____direc['templateDir'].DS.$____gets['controller'].DS.$____gets['action'].'.php';
			$____view_tpl_c = $____direc['compileDir'].DS.$____gets['controller'].DS.$____gets['action'].'.php';
			$____view_tpl_tag = $____gets['controller'].DS.$____gets['action'].'.php';
			
		//	debug::show(R::singleton());
			
			if(!file_exists($____view_tpl)){
				Errors::Go('404 - Not Found - '.$____gets['controller'].DS.$____gets['action']);
			}
			if (!factory::loadetpl()->compile($____view_tpl,$____view_tpl_c)) {
				Errors::Go('编译模板过程中出错!');
			}
			ob_start();
			extract(R::singleton()->data, EXTR_REFS);
			if (file_exists($____view_tpl_c)) {
				include_once($____view_tpl_c);
			} else {
				Errors::Go('没有发现编译后的模板:'.$____view_tpl_tag);
			}
			ob_end_flush();
			
		}
	}
  
	function gzip() {
		ini_set('zlib.output_compression', 1);
		ini_set('zlib.output_compression_level', 2);
	}
	
}
?>